<template>
    <div>
        <h1>{{id?`编辑`:`新建`}}项目</h1>
        <el-form label-width='120px'>
            <el-form-item label="项目名字"> 
                <el-input v-model="model.name"/>
            </el-form-item>
            <el-form-item label="需要人数"> 
                <el-input  v-model="model.need"/>
            </el-form-item>
             <el-form-item label="现在人数"> 
                <el-input  v-model="model.now"/>
            </el-form-item>
             <el-form-item label="最后人数"> 
                <el-input  v-model="model.end"/>
            </el-form-item>
            <el-form-item label="省份"> 
                <el-input  v-model="model.address"/>
            </el-form-item>
            <el-form-item label="地点"> 
                <el-input  v-model="model.didian"/>
            </el-form-item>
            <el-form-item label="类别"> 
                <el-input  v-model="model.leibie"/>
            </el-form-item>
             <el-form-item label="对象"> 
                <el-input  v-model="model.duixiang"/>
            </el-form-item>
             <el-form-item label="招募"> 
                <el-input  v-model="model.zhaomu"/>
            </el-form-item>
             <el-form-item label="时间"> 
                <el-input  v-model="model.time"/>
            </el-form-item>
            <el-form-item label="服务时间"> 
                <el-input  v-model="model.xxtime"/>
            </el-form-item>
            <el-form-item label="详情"> 
                <el-input  v-model="model.xq"/>
            </el-form-item>
            <el-form-item label='图片' style="margin-top:0.5rem">
                <el-upload 
                class="avatar-uploader"
                :action="uploadURL"
                :headers="getAuthHeaders()"
                :show-file-list="false"
                :on-success="res=>$set(model,'img',res.url)"
                >
                    <!-- 有图片显示图片，没有则显示上传图标,:src显示的图片 -->
                    <img v-if="model.img" :src="model.img" class="avatar"/>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type='primary' @click="save()" native-type='submit'>
                    保存
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    props:{
        id:{}
    },
    data(){
        return{
            model:{},
        }
    },
    methods:{
        async save(){
            let res
            if(this.id){
                res = await this.$http.put(`rest/Project/${this.id}`,this.model)
            }else{
                res = await this.$http.post('rest/Project',this.model)
            }
            this.$router.push('/project/list')
            this.$message({
                type:'success',
                message:'保存成功'
            })
        },
        async fetch(){
            const res = await this.$http.get('rest/Project/'+this.id)
            this.model = res.data
        },
        
    },
    created(){
        if(this.id){
            this.fetch()
        }
    }
}
</script>